<?php

namespace App\Miya\Tools;

use App\Miya\Models\Info;
use App\Miya\Models\UserView;
use Illuminate\Support\Facades\Cache;

class ClearTool
{
    protected $dirs;

    public function __construct()
    {
        $this->dirs = [
            'page-cache' => [
                'name' => '缓存页面',
                'path' => public_path('page-cache'),
                'url' => route(config('admin.route.prefix') . '.tools.clear_page_cache'),
            ],
            'logs' => [
                'name' => '系统日志',
                'path' => storage_path('logs'),
                'url' => route(config('admin.route.prefix') . '.tools.clear_logs'),
            ],
            'cache' => [
                'name' => '缓存数据',
                'path' => storage_path('framework/cache/data'),
                'url' => route(config('admin.route.prefix') . '.tools.clear_cache_data'),
            ],
            'views' => [
                'name' => '渲染页面',
                'path' => storage_path('framework/views'),
                'url' => route(config('admin.route.prefix') . '.tools.clear_views'),
            ],
            'dirty_data' => [
                'name' => '脏数据(无分类信息)',
                'path' => 'App/Miya/Models/Info',
                'url' => route(config('admin.route.prefix') . '.tools.clear_dirty_data'),
                'count' => Info::doesntHave('classify')->count()
            ],
            'uv_record' => [
                'name' => 'uv记录数据',
                'path' => 'App/Miya/Models/UserView',
                'url' => route(config('admin.route.prefix') . '.tools.clear_uv_record'),
                'count' => UserView::count()
            ],
        ];
    }

    /**
     * 获取文件夹大小数据
     * @return array
     */
    public function fileInfo()
    {
        $cacheKey = 'clear_data';
        return Cache::remember($cacheKey, 60, function () {
            $data = [];
            foreach ($this->dirs as $name => $value) {
                $real_size = $this->getDirSize($value['path']);

                $size = $this->getRealSize($real_size);
                $l = [
                    'name' => $value['name'],
                    'path' => $value['path'],
                    'real_size' => $real_size,
                    'size' => $size,
                    'url' => $value['url'] ?? '',
                ];
                if (!empty($value['count'])) {
                    $l['count'] = $value['count'];
                }

                $data[] = $l;
            }

            usort($data, function ($a, $b) {
                return $a['real_size'] <= $b['real_size'];
            });

            return $data;
        });
    }

    // 获取文件夹大小
    protected function getDirSize($dir, $sizeResult = 0)
    {
        if (!is_dir($dir)) {
            return 0;
        }
        $handle = opendir($dir);
        while (false !== ($FolderOrFile = readdir($handle))) {
            if ($FolderOrFile != "." && $FolderOrFile != "..") {
                if (is_dir("$dir/$FolderOrFile")) {
                    $sizeResult += $this->getDirSize("$dir/$FolderOrFile");
                } else {
                    $sizeResult += filesize("$dir/$FolderOrFile");
                }
            }
        }
        closedir($handle);
        return $sizeResult;
    }

    // 单位自动转换函数
    protected function getRealSize($size)
    {
        $kb = 1024;
        $mb = 1024 * $kb;
        $gb = 1024 * $mb;
        $tb = 1024 * $gb;
        if ($size < $kb) {
            return $size . " B";
        } else if ($size < $mb) {
            return round($size / $kb, 2) . " KB";
        } else if ($size < $gb) {
            return round($size / $mb, 2) . " MB";
        } else if ($size < $tb) {
            return round($size / $gb, 2) . " GB";
        } else {
            return round($size / $tb, 2) . " TB";
        }
    }
}
